package com.lpf.www.usercontroller;

import com.lpf.www.common.dto.ResponseMsg;
import com.lpf.www.common.entity.User;
import com.lpf.www.common.utils.CommonData;
import com.lpf.www.common.utils.DataHandler;
import com.lpf.www.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * @Author lpf
 * @Date 2021/12/30 下午12:20
 * @ProjectName live-streaming-os
 * @Version 1.0
 * @Description:
 */
@Controller
@RequestMapping("user")
public class UserController {

    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping(value = "userLogin")
    @ResponseBody
    public String userLogin(String username, String password) {
        ResponseMsg responseMsg = new ResponseMsg();
        if (DataHandler.isEmpty(username)) {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("用户名不能为空！");
        } else if (DataHandler.isEmpty(password)) {
            responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
            responseMsg.setResMsg("密码不能为空！");
        } else {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            responseMsg = userService.userLogin(user);
        }
        return DataHandler.getJsonString(responseMsg);
    }

    @PostMapping(value = "userRegister")
    @ResponseBody
    public String userRegister(String username, String password, String confirmPassword, String sex, String email, String phone, String verificationCode) {
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
        if (DataHandler.isEmpty(username)) {
            responseMsg.setResMsg("用户名不能为空！");
        } else if (DataHandler.isEmpty(password)) {
            responseMsg.setResMsg("密码不能为空！");
        } else if (DataHandler.isEmpty(email)) {
            responseMsg.setResMsg("邮箱不能为空！");
        } else if (DataHandler.isEmpty(phone)) {
            responseMsg.setResMsg("电话不能为空！");
        } else if (!password.equals(confirmPassword)) {
            responseMsg.setResMsg("两次输入密码不一致！");
        } else if (!DataHandler.isEmail(email)) {
            responseMsg.setResMsg("邮箱格式不正确！");
        } else if (!DataHandler.isPhone(phone)) {
            responseMsg.setResMsg("电话号码格式不正确！");
        } else if (DataHandler.isEmpty(verificationCode)){
            responseMsg.setResMsg("请输入验证码");
        }else {
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setSex(sex);
            user.setEmail(email);
            user.setPhone(phone);
            user.setRegisterDate(new Date());
            user.setAvatar("https://lanhuhen.oss-cn-chengdu.aliyuncs.com/images/34d84acbb20a4fa2a41e4d67900e6625_default.jpg");
            responseMsg = userService.insertUser(user, verificationCode);
        }
        return DataHandler.getJsonString(responseMsg);
    }

    @ResponseBody
    @PostMapping("getVerificationCode")
    public String getVerificationCode(String username, String password, String confirmPassword, String email, String phone){
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
        if (DataHandler.isEmpty(username) || DataHandler.isEmpty(password) || DataHandler.isEmpty(email) || DataHandler.isEmpty(phone)) {
            responseMsg.setResMsg("请完善以上信息！");
        } else if (!password.equals(confirmPassword)) {
            responseMsg.setResMsg("两次输入密码不一致！");
        } else if (!DataHandler.isEmail(email)) {
            responseMsg.setResMsg("邮箱格式不正确！");
        } else if (!DataHandler.isPhone(phone)) {
            responseMsg.setResMsg("电话号码格式不正确！");
        } else {
            responseMsg = userService.getVerificationCode(phone);
        }
        return DataHandler.getJsonString(responseMsg);
    }

    @ResponseBody
    @PostMapping("getVerificationCodeByPhone")
    public String getVerificationCodeByPhone(String phone){
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
        if (DataHandler.isEmpty(phone)) {
            responseMsg.setResMsg("请填写手机号！");
        }  else if (!DataHandler.isPhone(phone)) {
            responseMsg.setResMsg("电话号码格式不正确！");
        } else {
            responseMsg = userService.getVerificationCodeToLogin(phone);
        }
        return DataHandler.getJsonString(responseMsg);
    }

    @ResponseBody
    @PostMapping("loginByCode")
    public String loginByCode(String phone, String verificationCode){
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setResCode(CommonData.ILLEGAL_ERROR_CODE);
        if (DataHandler.isEmpty(phone)){
            responseMsg.setResMsg("电话不能为空！");
        } else if (DataHandler.isEmpty(verificationCode)){
            responseMsg.setResMsg("请输入验证码");
        } else if (!DataHandler.isPhone(phone)){
            responseMsg.setResMsg("电话号码格式不正确！");
        } else {
            responseMsg = userService.loginByCode(phone, verificationCode);
        }
        return DataHandler.getJsonString(responseMsg);
    }
}
